

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.dao.DiscoverDao;
import com.yz.dao.StationSettingDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sationSettingService")
public class StationSettingServiceImpl extends ServiceImpl<StationSettingDao,StationSettingEntity> implements StationSettingService {
    @Autowired
    CheckService checkService;
    @Autowired
    SettingDetailService settingDetailService;
    @Autowired
    ProjectService projectService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postStationSetting(StationSettingForm t) {
        StationSettingEntity data = t.getStationSettingEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(data.getUpdateAt());
        check.setCreateUser(data.getCreateUser());
        check.setName(projectService.selectById(data.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[6]);
        check.setProjectId(data.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(103);
        long id = 0;
        if(data.getId()==0){
            this.baseMapper.insert(data);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[6]+id);
            checkService.insert(check);
        }else{
           id = data.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[6]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[6]+id));
            this.baseMapper.update(data,new EntityWrapper<StationSettingEntity>().eq("id",id));
            settingDetailService.delete(new EntityWrapper<StationDetailEntity>().eq("station_id",id));
        }
        List<StationDetailEntity> collections = t.getStationDetailEntities();
        for (int i = 0; i < collections.size(); i++) {
            StationDetailEntity item = collections.get(i);
            item.setStationId(id);
            settingDetailService.insert(item);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public StationSettingForm getSettingDetail(long id) {
        StationSettingForm data = new StationSettingForm();
    StationSettingEntity t = new StationSettingEntity();
    t.setId(id);
        List<StationDetailEntity> collections = settingDetailService.selectList(new EntityWrapper<StationDetailEntity>().eq("station_id",id).groupBy("id"));
        data.setStationSettingEntity(this.baseMapper.selectById(t));
        data.setStationDetailEntities(collections);
        return data;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteStationSetting(long id) {
        this.deleteById(id);
        this.baseMapper.deleteSetting(id);
        checkService.deleteMe(5,103,id);
    }
}
